package caiqr.utils

import java.sql.DriverManager
import org.apache.spark.rdd.RDD

object PredictionDBUtils {

  // 成功运行结束:
  def save_result_to_mysql(model_run_result: String, save_db_info: String, job_id: String, spark_id: String): Unit ={

    //结果集保存DB信息: IP-DB名称-user-passwd-表名称-字段type的名称 (表示那个模型, 那个版本 varchar类型)-字段key的名称 (varchar类型)-字段value的名称 (varchar类型)-字段type的值
    val split_items_arr = save_db_info.split("-")
    val db_ip = split_items_arr(0)
    val db_name = split_items_arr(1)
    val db_user = split_items_arr(2)
    val db_passwd = split_items_arr(3)

    //val url = "jdbc:mysql://192.168.100.100:3306/jose"
    //val username = "caiqiu"
    //val password = "Caiqiu502"
    val url = s"jdbc:mysql://${db_ip}:3306/${db_name}"
    val username = db_user
    val password = db_passwd
    Class.forName("com.mysql.jdbc.Driver").newInstance
    val conn = DriverManager.getConnection(url,username,password)

    // 保存计算结果
    val updateResultData = conn.prepareStatement("UPDATE job set model_result=?, status=3 where id=?")
    updateResultData.setString(1, model_run_result)
    updateResultData.setInt(2, job_id.toInt)
    updateResultData.executeUpdate

    // 更新 spark 使用状态
    val updateSparkResultData = conn.prepareStatement("UPDATE spark set status=0 where id=?")
    updateSparkResultData.setInt(1, spark_id.toInt)
    updateSparkResultData.executeUpdate


    conn.close()
  }



  // 成功运行结束:
  def update_job_spark_status(save_db_info: String, job_id: String, spark_id: String): Unit ={

    //结果集保存DB信息: IP-DB名称-user-passwd-表名称-字段type的名称 (表示那个模型, 那个版本 varchar类型)-字段key的名称 (varchar类型)-字段value的名称 (varchar类型)-字段type的值
    val split_items_arr = save_db_info.split("-")
    val db_ip = split_items_arr(0)
    val db_name = split_items_arr(1)
    val db_user = split_items_arr(2)
    val db_passwd = split_items_arr(3)

    //val url = "jdbc:mysql://192.168.100.100:3306/jose"
    //val username = "caiqiu"
    //val password = "Caiqiu502"
    val url = s"jdbc:mysql://${db_ip}:3306/${db_name}"
    val username = db_user
    val password = db_passwd
    Class.forName("com.mysql.jdbc.Driver").newInstance
    val conn = DriverManager.getConnection(url,username,password)

    // 保存计算结果
    val updateResultData = conn.prepareStatement("UPDATE job set status=3 where id=?")
    updateResultData.setInt(1, job_id.toInt)
    updateResultData.executeUpdate

    // 更新 spark 使用状态
    val updateSparkResultData = conn.prepareStatement("UPDATE spark set status=0 where id=?")
    updateSparkResultData.setInt(1, spark_id.toInt)
    updateSparkResultData.executeUpdate


    conn.close()
  }





  // 成功运行结束:
  def update_job_spark_status2(save_db_info: String, job_id: String, spark_id: String): Unit ={

    //结果集保存DB信息: IP-DB名称-user-passwd-表名称-字段type的名称 (表示那个模型, 那个版本 varchar类型)-字段key的名称 (varchar类型)-字段value的名称 (varchar类型)-字段type的值
    val split_items_arr = save_db_info.split("-")
    val db_ip = split_items_arr(0)
    val db_name = split_items_arr(1)
    val db_user = split_items_arr(2)
    val db_passwd = split_items_arr(3)

    //val url = "jdbc:mysql://192.168.100.100:3306/jose"
    //val username = "caiqiu"
    //val password = "Caiqiu502"
    val url = s"jdbc:mysql://${db_ip}:3306/${db_name}"
    val username = db_user
    val password = db_passwd
    Class.forName("com.mysql.jdbc.Driver").newInstance
    val conn = DriverManager.getConnection(url,username,password)

    // 保存计算结果
    val updateResultData = conn.prepareStatement("UPDATE job set status=5 where id=?")
    updateResultData.setInt(1, job_id.toInt)
    updateResultData.executeUpdate

    // 更新 spark 使用状态
    val updateSparkResultData = conn.prepareStatement("UPDATE spark set status=0 where id=?")
    updateSparkResultData.setInt(1, spark_id.toInt)
    updateSparkResultData.executeUpdate


    conn.close()
  }



}


